package com.tingyukeji.fastpay.sport.ui.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tingyukeji.fastpay.R;
import com.tingyukeji.fastpay.base.BaseFragment;
import com.tingyukeji.fastpay.databinding.StepFragmentBinding;
import com.tingyukeji.fastpay.sport.view.RiseNumberTextView;
import com.tingyukeji.fastpay.step.bean.StepMessage;
import com.tingyukeji.fastpay.utils.SharedPreferencesUtils;
import com.mcxiaoke.bus.annotation.BusReceiver;

/**
 * Created by Administrator on 2016/11/10.
 */

public class StepFragment extends BaseFragment implements View.OnClickListener{
    private StepFragmentBinding binding;
    private SharedPreferencesUtils sp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            binding = DataBindingUtil.inflate(inflater, R.layout.step_fragment, container, false);
            rootView = binding.getRoot();
            initView();
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        initData();
        return rootView;
    }

    @BusReceiver
    public void stepEvent(StepMessage stepMessage) {
        String planWalk_QTY = (String) sp.getParam("planWalk_QTY", "2000");
        binding.cc.setCurrentCount(Integer.parseInt(planWalk_QTY), stepMessage.getStepNum());
        binding.tvRiseNum.setText(stepMessage.getStepNum()+"");
    }

    @BusReceiver
    public void stepEvent1(Integer stepNum) {
        sp = new SharedPreferencesUtils(getActivity());
        //获取用户设置的计划锻炼步数，没有设置过的话默认7000
        String planWalk_QTY = (String) sp.getParam("planWalk_QTY", "2000");
        //设置当前步数为0
        binding.cc.setCurrentCount(Integer.parseInt(planWalk_QTY), stepNum);
        binding.tvIsSupport.setText("最近同步:刚刚");
        animText(binding.tvRiseNum, (int)stepNum);
    }

    /**
     * 给一个TextView设置一个数字增长动画
     */
    public void animText(RiseNumberTextView tv, int number) {
        // 设置数据
        tv.withNumber(number);
        // 设置动画播放时间
        tv.setDuration(1500);
        tv.start();
    }

    @Override
    protected void initView() {
        super.initView();
    }


    private void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
        }
    }
}
